package com.czy.owner.ui.workorder.orderviewholder;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.api.OrderCancleApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.MyOrderModel;
import com.czy.owner.entity.WorkOrderModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.store.OnLineOrderActivity2;
import com.czy.owner.ui.workorder.BesPeakDetailsActivity;
import com.czy.owner.ui.workorder.SubscribeOrdelActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.CountdownView;
import com.czy.owner.widget.UniversalDialog;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

/**
 * Created by hedepu on 2017-08-28
 */

public class OrderPendingServiceViewHolder extends BaseViewHolder<WorkOrderModel> {

    private Context ctx;
    private TextView tv_order_number, tv_order_state, tv_order_num, tv_order_item, tv_order_service_time, tv_order_jishi, tv_order_dingjing, tv_store_name;
    private TextView btn_order_paid;
    private LinearLayout linear_order_num;
    private CountdownView cv_seckill;

    public OrderPendingServiceViewHolder(ViewGroup parent, Context ctx) {
        super(parent, R.layout.item_order_wait_service);
        tv_order_number = $(R.id.tv_order_number);
        tv_order_state = $(R.id.tv_order_state);
        linear_order_num = $(R.id.linear_order_num);
        tv_order_num = $(R.id.tv_order_num);
        tv_order_item = $(R.id.tv_order_item);
        tv_order_service_time = $(R.id.tv_order_service_time);
        tv_order_jishi = $(R.id.tv_order_jishi);
        tv_order_dingjing = $(R.id.tv_order_dingjing);
        btn_order_paid = $(R.id.btn_order_paid);
        cv_seckill = $(R.id.cv_seckill);
        tv_store_name = $(R.id.tv_store_name);
        this.ctx = ctx;
    }

    @Override
    public void setData(final WorkOrderModel model) {
        tv_store_name.setText(model.getStoreName());
        tv_order_number.setText("工单编号: " + model.getOrderNumber());
        if (model.getStoreBookingNumber() != null && !model.getStoreBookingNumber().isEmpty()) {
            if (linear_order_num.getVisibility() == View.GONE)
                linear_order_num.setVisibility(View.VISIBLE);
            tv_order_num.setText(model.getStoreBookingNumber());
        } else {
            if (linear_order_num.getVisibility() == View.VISIBLE)
                linear_order_num.setVisibility(View.GONE);
        }
//        tv_order_service_time.setText(TimeUtils.timeStamp2Date(model.getServiceTime(), "yyyy-MM-dd HH:mm"));
//        String daytime = TimeUtils.timeStamp2Date(model.getServiceTime(), "HH");
//        String huor;
//        if (Integer.parseInt(daytime) > 12) {
//            huor = "上午";
//        } else {
//            huor = "下午";
//        }
//        tv_order_service_time.setText(TimeUtils.timeStamp2Date(model.getServiceTime(), "yyyy-MM-dd") + " " + TimeUtils.getWeeky(Long.parseLong(model.getServiceTime()))
//                + huor);
        tv_order_service_time.setText(model.getConvertServiceTime());
        tv_order_item.setText(StringUtils.getString(model.getOrderItemNameStr(), "无"));
        tv_order_jishi.setText(model.getLicenseNumber() + "  " + model.getChName());
//        tv_order_dingjing.setText("¥" + DecimalUtil.formatNum(model.getPayedDepositAmount() + ""));
        tv_order_dingjing.setText("¥" + DecimalUtil.formatNum(model.getTotalPrice()));
        btn_order_paid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(model);
            }
        });
    }

    private void showDialog(final WorkOrderModel model) {
        if (model.getPayedDepositAmount() != 0) {
            //待服务
            new UniversalDialog(ctx).builder()
                    .setCancelable(false)
                    .setMsg("取消预约，订金将全部原路退回，\n请及时查收~")
                    .setPositiveButton("我再想想", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    }).setNegativeButton("执意离去", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCancle(model.getUserOrderId(), model.getStoreId());

                }
            }).show();
        } else {
            //待付款，和0元订金的
            new UniversalDialog(ctx).builder()
                    .setCancelable(false)
                    .setMsg("确定取消预约？")
                    .setPositiveButton("我再想想", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    }).setNegativeButton("执意离去", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCancle(model.getUserOrderId(), model.getStoreId());

                }
            }).show();
        }
//        //待服务
//        new UniversalDialog(ctx).builder()
//                .setCancelable(false)
//                .setMsg("取消预约，订金将全部原路退回，\n请及时查收~")
//                .setPositiveButton("我再想想", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                    }
//                }).setNegativeButton("执意离去", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                setCancle(model.getUserOrderId());
//
//            }
//        }).show();
    }

    //取消预约
    private void setCancle(String userOrderId, int storeId) {
        OrderCancleApi orderCancleApi = new OrderCancleApi(new HttpOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                Intent intent = new Intent();
                intent.setAction(SubscribeOrdelActivity.orderAction);
                intent.putExtra("currentId", 0);
                ctx.sendBroadcast(intent);
                PhoneUtils.ShowToastMessage(ctx, "取消预约成功");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
                PhoneUtils.ShowToastMessage(ctx, "取消预约失败");
            }
        }, (RxAppCompatActivity) ctx);
        ;
        orderCancleApi.setSession(UserHelper.getInstance().getSessionInfoModel(ctx).getSession());
        orderCancleApi.setStoreId(storeId + "");
        orderCancleApi.setUserOrderId(userOrderId);
        HttpManager.getInstance().doHttpDeal(orderCancleApi);
    }

}
